package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowNodeCandidateService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
　　* @description: 流程任务节点处理候选人实现
　　* @author shenrm
　　* @date 2021/11/29 17:10
　　*/
@Service("flowTaskNodeCandidateService")
public class FlowTaskNodeCandidateServiceImpl implements FlowNodeCandidateService {

    @Autowired
    private HistoryService historyService;

    @Override
    public String getFlowNodeCandidateUser(String processInstanceId) {

        if(StringUtils.isBlank(processInstanceId)) {
            return null;
        }

        // 根据流程实例ID获取整个流程实例第一个任务节点的处理人
        List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).orderByTaskCreateTime().asc().list();

        // 为空时,返回并报错
        if(CollectionUtils.isEmpty(historyTasks)) {
            return null;
        }
        return historyTasks.get(0).getAssignee();
    }
}
